package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.CustomException;
import com.itheima.dao.DishFlavorMapper;
import com.itheima.dao.DishMapper;
import com.itheima.domain.Dish;
import com.itheima.domain.DishFlavor;
import com.itheima.dto.DishDto;
import com.itheima.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DishServiceImpl
        extends ServiceImpl<DishMapper, Dish>
        implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;


    //DishDto是json格式
    public void saveDish(DishDto dishDto) {
        //保存菜品数据
        dishMapper.insert(dishDto);

        List<DishFlavor> flavors = dishDto.getFlavors();
        //遍历方法一
        if (flavors != null && flavors.size() > 0) {
            //保存菜品的口味数据
            Long dishId = dishDto.getId();  //没有数据，需要设置
            //设置dishId值
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishId);
                dishFlavorMapper.insert(flavor);
            }

            //遍历方法二
//            flavors.stream().map((flavor) -> {
//                flavor.setDishId(dishId);
//                return flavor;
//            }).collect(Collectors.toList());

        }

    }


    /**
     * 批量删除
     *
     * @param ids
     */
    @Override
    public void delectByIdDish(List<Long> ids) {
        // 删除id,批量删除是多个id需要循环
        for (Long aLong : ids) {
            //如果id值为空,则删除失败
            if (aLong == null) {
                throw new CustomException("删除失败!");
            }
            //id存在,则删除
            dishMapper.deleteById(aLong);
        }

    }


    /**
     * 启用禁用
     *
     * @param status
     * @param ids
     */
    @Override
    public void updateDish(Integer status, List<Long> ids) {
        //遍历id
        for (Long id : ids) {
            //判断id是否为空
            if (id != null) {
                //创建一个对象
                Dish dish = new Dish();
                //设置id值
                dish.setId(id);
                //设置status值
                dish.setStatus(status);
                //按照id修改dish中的值
                dishMapper.updateById(dish);

            }

        }




    }

}